package com.jwmp.rbac.web.servlet;

import com.jwmp.core.servlet.BaseServlet;
import com.jwmp.core.util.PageResult;
import com.jwmp.core.util.QueryObject;
import com.jwmp.core.util.QueryObjectUtil;
import com.jwmp.core.util.StringUtil;
import com.jwmp.rbac.domain.Department;
import com.jwmp.rbac.domain.Employee;
import com.jwmp.rbac.service.IDepartmentService;
import com.jwmp.rbac.service.impl.DepartmentServiceImpl;
import com.jwmp.rbac.util.SetPermission;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;


@WebServlet("/department/*")
public class DepartmentServlet extends BaseServlet {
    private IDepartmentService departmentService = new DepartmentServiceImpl();

    @SetPermission(moduleName = "部门管理",name="部门列表",expression = "/department/list")
    public void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        QueryObject qo = QueryObjectUtil.req2qo(req);
        PageResult pageResult = departmentService.selectList(qo);
        req.setAttribute("pageResult",pageResult);
        req.getRequestDispatcher("/WEB-INF/views/rbac/department_list.jsp").forward(req,resp);
    }

    @SetPermission(moduleName = "部门管理",name="部门编辑页面",expression = "/department/edit")
    public void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String strId = req.getParameter("id");
        if (StringUtil.hasLength(strId)) {
            Department department = departmentService.selectOne(Long.valueOf(strId));
            req.setAttribute("department",department);
        }
        req.getRequestDispatcher("/WEB-INF/views/rbac/department_input.jsp").forward(req,resp);
    }

    @SetPermission(moduleName = "部门管理",name="部门保存或修改",expression = "/department/saveOrUpdate")
    public void saveOrUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Department dept = new Department();
        req2department(req,dept);
        if (dept.getId() == null) {
            departmentService.insert(dept);
        }else{
            departmentService.update(dept);
        }
        list(req,resp);
    }

    @SetPermission(moduleName = "部门管理",name="启用和注销",expression = "/department/updateStatus")
    public void updateStatus(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String strId = req.getParameter("id");
        String strStatus = req.getParameter("status");
        if (StringUtil.hasLength(strId) && StringUtil.hasLength(strStatus)) {
            departmentService.updateStatus(Long.valueOf(strId),Boolean.valueOf(strStatus));
        }

        list(req,resp);
    }

    private void req2department(HttpServletRequest req, Department dept) {
        String strId = req.getParameter("id");
        if (StringUtil.hasLength(strId)) {
            dept.setId(Long.valueOf(strId));
        }
        String name = req.getParameter("name");
        dept.setName(name);
    }

    @SetPermission(moduleName = "部门管理",name="部门员工管理页面",expression = "/department/editEmployee")
    public void editEmployee(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String strId = req.getParameter("id");
        if (StringUtil.hasLength(strId)) {
            List<Employee> existEmpList = departmentService.selectExistEmpsByDeptId(Long.valueOf(strId));
            List<Employee> notExistEmpList = departmentService.selectNotExistEmpsByDeptId(Long.valueOf(strId));
            Department dept = departmentService.selectOne(Long.valueOf(strId));
            req.setAttribute("existEmpList",existEmpList);
            req.setAttribute("notExistEmpList",notExistEmpList);
            req.setAttribute("dept",dept);
        }

        req.getRequestDispatcher("/WEB-INF/views/rbac/department-employee.jsp").forward(req,resp);
    }

    @SetPermission(moduleName = "部门管理",name="部门绑定员工",expression = "/department/bindEmployee")
    public void bindEmployee(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String[] empIds = req.getParameterValues("empId");
        String strDeptId = req.getParameter("id");
        if (StringUtil.hasLength(strDeptId)) {
            List<Long> newEmpIdList = empIds != null
                        ? Arrays.stream(empIds).filter(strEmpId -> StringUtil.hasLength(strEmpId))
                                                .map(strEmpId -> Long.valueOf(strEmpId))
                                                .collect(Collectors.toList())
                        : new ArrayList<Long>();
            departmentService.bindEmployee(Long.valueOf(strDeptId),newEmpIdList);
        }
        editEmployee(req,resp);
    }

}
